from blueprints.person import person
from flask import render_template
from flask import render_template, request, make_response
from blueprints.globalReference import db_helper
import json
from blueprints.globalReference import User,login_manager
from flask_login import login_user,login_required,current_user

@person.route("/test")
def test():
    return render_template("test1.html")

@person.route('/grzx', methods=['GET'])
@login_required
def grzx():
    """
    个人中心
    :return:
    """
    try:
        student_id = current_user.id
        sql = "select id,name,student_id,zy,jf from gmq_bs_student where student_id = '"+str(student_id)+"'"
        student_datas = db_helper.getSelectSql(sql)
        student_data = [{'id': data[0], 'name': data[1], 'student_id': data[2], 'zy': data[3],'jf':data[4]} for data in student_datas]

        sql="select state,yggz,wggz,hmdgz from gmq_bs_sygz"
        sygz_datas = db_helper.getSelectSql(sql)
        sygz_data = [{'state': data[0], 'yggz': data[1], 'wggz': data[2], 'hmdgz': data[3]} for data in sygz_datas]

        sql = "select name,sc,cs from gmq_bs_sygz_blgz"
        blgz_datas = db_helper.getSelectSql(sql)
        blgz_data = [{'name': data[0], 'sc': data[1], 'cs': data[2]} for data in blgz_datas]

        resp = make_response(
            render_template(
                             'grzx.html',student_datas=student_data,sygz_data=sygz_data,blgz_data=blgz_data))
    except Exception as e:
        resp = make_response(
            render_template('warn.html',errormsg=('出错了，错误码：%s ' % e)))
        return resp
    return resp

@person.route('/feedback', methods=['GET'])
@login_required
def feedback():
    """
    意见反馈
    :return:
    """
    try:
        student_id = current_user.id
        sql = "select id,name,student_id,zy,jf from gmq_bs_student where student_id = '"+str(student_id)+"'"
        student_datas = db_helper.getSelectSql(sql)
        student_data = [{'id': data[0], 'name': data[1], 'student_id': data[2], 'zy': data[3],'jf':data[4]} for data in student_datas]
        s_id = student_data[0]['id']

        feedback = request.args['feedback']
        sql = "INSERT INTO gmq_bs_fkb (student_ref,remark) values ('" + str(s_id) + "','" + feedback + "')"
        db_helper.updateSql(sql)
        rs = {'data': 'success'}
        return json.dumps(rs)
    except Exception as e:
        resp = make_response(
            render_template('warn.html',errormsg=('出错了，错误码：%s ' % e)))
        return resp
    return resp